package cheme.ProcessModel.ProcessUnits.Streams.Tags;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.IPropertyAware;
import cheme.ProcessModel.IXmlOperations;
import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.ModelStringLiterals;
import cheme.ProcessModel.XmlHelper;
import cheme.ProcessModel.Visitors.IModelVisitor;

public class TagComponent extends AbstractTagComponent implements IGeneralTagComponent
{
	/*
	 * Serialization version identifier. (Set to default for now)
	 */
	
	private static final long serialVersionUID = 1L;
	
	/*
	 * Default indices of this model element's primary children.
	 */
	
//	public static final int MATERIAL_INDEX = 3;
	
	public TagComponent(IModelElement parent)
	{
		super(parent);
	}
	
	protected void defaultInit()
	{
		super.defaultInit();
		
		ComponentUnitsField units = new ComponentUnitsField(this, 0);
		units.setToolTipText("The unit of measurement for this stream component.");
		add(units, UNITS_INDEX);
		
		MaterialField material = new MaterialField(this, 0);
		material.setToolTipText("The material of this stream component.");
		add(material, MATERIAL_INDEX);		
		
		HeatCapacityField heatCapField = new HeatCapacityField(this, DEFAULT_QTY_VALUE);
		heatCapField.setToolTipText("The heat capacity of the stream component.");
		add(heatCapField, HEAT_CAPACITY_INDEX);
		
		HeatUnitsField heatCapUnits = new HeatUnitsField(this, 0);
		heatCapUnits.setToolTipText("The units for the heat capactiy.");
		heatCapUnits.addOption(ModelStringLiterals.Percantage, 0);
		heatCapUnits.setSelection(0);
		add(heatCapUnits, HEAT_CAPACITY_UNITS_INDEX);

		LatentHeatCapacityField latentHeatCapField = new LatentHeatCapacityField(this, DEFAULT_QTY_VALUE);
		latentHeatCapField.setToolTipText("The latent heat capacity of the stream component.");
		add(latentHeatCapField, LATENT_HEAT_CAPACITY_INDEX);

		HeatUnitsField latentHeatCapUnits = new HeatUnitsField(this, 0);
		latentHeatCapUnits.setToolTipText("The units for the latent heat capactiy.");
		latentHeatCapUnits.addOption(ModelStringLiterals.Percantage, 0);
		latentHeatCapUnits.setSelection(0);
		add(latentHeatCapUnits, LATENT_HEAT_CAPACITY_UNITS_INDEX);
		
		if(material instanceof IXmlOperations)
		{
			material.toXml();
		}
		
	}
	
	public void setInitial(boolean initial)
	{
		isInitial = initial;
		firePropertyChange(P_INITIAL, null, initial);
	}
	
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		/*
		 * Make sure the interpreter can listen to my changes
		 */

		ModelEnv.getInstanceFor(this).hookModelElement(this);
		
		/*
		 * Make sure I can listen to my children created form a past session.
		 */

		for(IChild child : getChildren())
		{
			child.addPropertyChangeListener(this);
		}
	}
	
	/**
	 * @return the MATERIAL
	 */
	public String getMaterial()
	{
		return get(MATERIAL_INDEX).getText();
	}
	
	/**
	 * @param chemical the chemical to set
	 */
	public void setMaterial(String material)
	{
		set(MATERIAL_INDEX, material.trim(), IPropertyAware.P_MATERIAL);
	}
	
	public Object accept(IModelVisitor ruleChecker)
	{
		return ruleChecker.visit(this);
	}
	
	public String toXml() {
		
		// we must modify "type" attribute from the
		//base class.  We need to make sure that the change
		//is reflected in our XML
		String xml = super.toXml();
		Document doc = (Document)XmlHelper.xmlFromString(xml);
		Element e1 = doc.getDocumentElement();
		e1.setAttribute("type", "TagComponent");
		
		xml = XmlHelper.xmlToString(doc);
		return xml;
	}
}
